package cn.woniu.ecommerceplatform.service.impl;

import cn.woniu.ecommerceplatform.mapper.GoodsMapper;
import cn.woniu.ecommerceplatform.util.ResponseResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.woniu.ecommerceplatform.entity.Comments;
import cn.woniu.ecommerceplatform.service.CommentsService;
import cn.woniu.ecommerceplatform.mapper.CommentsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @author QIQI
* @description 针对表【t_comments】的数据库操作Service实现
* @createDate 2023-01-09 11:50:37
*/
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments>
    implements CommentsService{
    @Autowired(required = false)
    private CommentsMapper commentsMapper;
    @Autowired(required = false)
    private GoodsMapper goodsMapper;
    @Override
    public ResponseResult<?> insertComments(Comments comments) {
        try {
            int count = commentsMapper.insert(comments);
            if(count>0){
                return new ResponseResult<>().ok();
            }else{
                return new ResponseResult<>().notok();
            }
        } catch (Exception e) {
            return new ResponseResult<>().notok("请完善信息后，再进行添加");
        }
    }

    @Override
    public ResponseResult<?> updateComments(Comments comments) {
        int count = commentsMapper.updateById(comments);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> deleteComments(Integer id) {
        int count = commentsMapper.deleteById(id);
        if(count>0){
            return new ResponseResult<>().deleteSuccess((long) count);
        }else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> deleteCommentsBranchs(Integer[] ids) {
        List idlist =  new ArrayList<Integer>(Arrays.asList(ids));
        int count = commentsMapper.deleteBatchIds(idlist);
        if(count>0){
            return new ResponseResult<>().deleteSuccess((long) count);
        }else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> querylist(String goodCode, String customerName, String keywords) {
        List<Comments> comments = commentsMapper.queryCommentslist(goodCode, customerName, keywords);
        if(!comments.isEmpty()){
            return new ResponseResult<>().ok(comments);
        }else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> changeVisiable(Integer id) {
        int count = commentsMapper.changeVisiable(id);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> changeTop(Integer id) {
        int count = commentsMapper.changeTop(id);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> commentsAdd(Comments comments) {
        comments.setCommentsTime(new Date());
        int count = commentsMapper.commentsAdd(comments);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }
}




